package com.fuyun.microservice.proxy;

import com.fuyun.microservice.proxy.forward.RibbonRestRestForward;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.apache.HttpClientRibbonCommandFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Set;


@Component
@Configuration
public class ProxyConfiguration {



    @Autowired(required = false)
    private List<RibbonRequestCustomizer> requestCustomizers = Collections.emptyList();

    @Autowired(required = false)
    private Set<ZuulFallbackProvider> zuulFallbackProviders = Collections.emptySet();

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public RibbonCommandFactory ribbonCommandFactory(
            SpringClientFactory clientFactory) {
        return new HttpClientRibbonCommandFactory(clientFactory, new ZuulProperties(), zuulFallbackProviders);
    }

    @Bean
    public RibbonRestRestForward ribbonRestForward(RibbonCommandFactory<?> ribbonCommandFactory) {
        return new RibbonRestRestForward(new ProxyRequestHelper(), ribbonCommandFactory,requestCustomizers);
    }

    @Bean
    public DiscoveryRoute discoveryRoute(){
        return new DefaultForwardRoute();
    }
}
